package com.xm.servlet.admin;

import com.xm.biz.GoodsBiz;
import com.xm.biz.GoodsTypeBiz;
import com.xm.biz.impl.GoodsBizImpl;
import com.xm.biz.impl.GoodsTypeBizImpl;
import com.xm.po.Goods;
import com.xm.util.JsonUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @create 2020-08-27 14:16
 * @desc 商品管理处理器
 **/
@WebServlet("/commodity")
public class CommodityServlet extends HttpServlet {
    //初始化数据源
    GoodsBiz goodsBiz = new GoodsBizImpl();
    GoodsTypeBiz goodsTypeBiz = new GoodsTypeBizImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        String type = req.getParameter("type");
        req.setAttribute("goodsTypeList",goodsTypeBiz.getAll());
        //所有商品信息(查询)
        if (type.equals("goodsInfo")){
            goodsInfo(req,resp);
        }
        //根据编号或名字查询商品信息(搜索按钮)
        else if (type.equals("search")){
            search(req,resp);
        }
        //根据编号查询商品信息
        else if (type.equals("goodsById")){
            goodsById(req, resp);
        }
        //修改商品信息
        else if (type.equals("updateGoods")){
            try {
                updateGoods(req, resp);
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        //商品下架
        else if (type.equals("soldOut")){
            soldOut(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
    //所有商品信息(查询)
    public void goodsInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Goods> goodsList = goodsBiz.getAll();
        //查询所有商品类型
        req.setAttribute("goodsTypeList",goodsTypeBiz.getAll());
        req.setAttribute("goodsList",goodsList);
        req.getRequestDispatcher("admin/goods.jsp").forward(req,resp);
    }
    //根据编号或名字查询商品信息(搜索按钮)
    public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String goodsId = req.getParameter("goodsId");
        String goodsName = req.getParameter("goodsName");
        String account = req.getParameter("goodsType");
        List<Goods> list = new ArrayList<>();
        if (!goodsId.equals("")&&goodsName.equals("")){
            //根据商品编号查询
            Goods goods = goodsBiz.queryById(goodsId);
            list.add(goods);
            req.setAttribute("goodsList",list);
            req.setAttribute("goodsId",goodsId);
            req.getRequestDispatcher("admin/goods.jsp").forward(req,resp);
        }
        else if (goodsId.equals("")&&!goodsName.equals("")){
            //根据商品名称查询
            Goods goods = goodsBiz.queryByName(goodsName);
            list.add(goods);
            req.setAttribute("goodsList",list);
            req.setAttribute("goodsName",goodsName);
            req.getRequestDispatcher("admin/goods.jsp").forward(req,resp);
        }
        else if (!account.equals("")&&goodsId.equals("")&goodsName.equals("")&&!account.equals("0")){
            //根据商品类型查询
            List<Goods> goods = goodsBiz.queryByGoodsType(account);
            req.setAttribute("goodsList",goods);
            req.getRequestDispatcher("admin/goods.jsp").forward(req,resp);
        }
        else if (!goodsId.equals("")&&!goodsName.equals("")&&account.equals("0")){
            //根据编号和名字查询商品
            List<Goods> goods = goodsBiz.queryOne(goodsId,goodsName);
            req.setAttribute("goodsList",goods);
            req.setAttribute("goodsId",goodsId);
            req.setAttribute("goodsName",goodsName);
            req.getRequestDispatcher("admin/goods.jsp").forward(req,resp);
        }
        else {
            List<Goods> goodsList = goodsBiz.getAll();
            //查询所有商品类型
            req.setAttribute("goodsTypeList",goodsTypeBiz.getAll());
            req.setAttribute("goodsList",goodsList);
            req.getRequestDispatcher("admin/goods.jsp").forward(req,resp);
        }
    }
    //商品下架
    public void soldOut(HttpServletRequest req, HttpServletResponse resp){
        String goodsId = req.getParameter("goodsId");
        boolean b = goodsBiz.soldOut(Integer.valueOf(goodsId));

    }
    //根据编号查询商品信息
    public void goodsById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String goodsId = req.getParameter("goodsId");
        Goods goodsByGoodsId = goodsBiz.getGoodsByGoodsId(Integer.valueOf(goodsId));

        PrintWriter out = resp.getWriter();
        out.print(JsonUtils.objectToJson(goodsByGoodsId));
        out.flush();
        out.close();
    }
    //修改商品信息
    public void updateGoods(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, IllegalAccessException, IOException {
        Goods goods = new Goods();
        BeanUtils.populate(goods,req.getParameterMap());
        boolean update = goodsBiz.update(goods);
        PrintWriter out = resp.getWriter();
        if (update==true){
            out.print("<script>location.href='commodity?type=goodsInfo'</script>");
            out.flush();
            out.close();
        }else {
            out.print("<script>alert('修改失败。');location.href='commodity?type=goodsInfo'</script>");
            out.flush();
            out.close();
        }

    }
}
